//
//  NSURL+CZURL.m
//  MethodSwizzleDemo
//
//  Created by czera on 2020/4/16.
//  Copyright © 2020 czera. All rights reserved.
//

#import "NSURL+CZURL.h"
#import <objc/runtime.h>

@implementation NSURL (CZURL)

+(void)load{
    // 获取方法
    Method urlWithStr = class_getClassMethod(self, @selector(URLWithString:));
    
    Method czUrlWithStr = class_getClassMethod(self, @selector(CZUrlWithString:));
    
    // 交换方法
    method_exchangeImplementations(urlWithStr, czUrlWithStr);
}

+(instancetype)CZUrlWithString:(NSString *)str{
    // 调用系统原来的方法
    NSURL *url = [NSURL CZUrlWithString:str];
    // 判断url是否为空，转码
    if (url == nil) {
        str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    }
    url = [NSURL CZUrlWithString:str];
    
    return url;
}

@end
